#![allow(unused_doc_comments)]
extern crate alloc;

use lazy_static::lazy_static;
use rbatis::RBatis;
use rbatis::rbdc::DateTime;
use rbatis::snowflake::Snowflake;
use rbdc_mysql::MysqlDriver;
use std::sync::LazyLock;
use std::time::SystemTime;

/// 使用第三方宏延迟初始化静态变量
lazy_static! {
    static ref SNOW_FLAKE: Snowflake = {
        // 2025-01-01 00:00:00
        let start_time = DateTime::from_timestamp(1735689600);
        let time = SystemTime::from(start_time);
        Snowflake::with_epoch(1, 1, 0, time)
    };
}

/// 使用标准库延迟初始化静态变量
#[allow(dead_code)]
static ID: LazyLock<Snowflake> = LazyLock::new(|| {
    // 2025-01-01 00:00:00
    let start_time = DateTime::from_timestamp(1735689600);
    let time = SystemTime::from(start_time);
    Snowflake::with_epoch(1, 1, 0, time)
});

/// 雪花算法生成id
pub fn gen_id() -> i64 {
    SNOW_FLAKE.generate()
}

/// 获取`rbatis`执行器
pub fn get_executor() -> RBatis {
    let rb = RBatis::new();
    // connect to database
    // mysql
    rb.init(MysqlDriver {}, "mysql://root:root@localhost:3306/rbatis")
        .unwrap();
    rb
}
